package com.crr.mall.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.crr.mall.common.to.SkuRelationTO;
import com.crr.mall.common.utils.Assert;
import com.crr.mall.common.utils.BaseController;
import com.crr.mall.coupon.dao.MemberPriceDao;
import com.crr.mall.coupon.dao.SkuBoundsDao;
import com.crr.mall.coupon.dao.SkuLadderDao;
import com.crr.mall.coupon.entity.SkuLadderEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crr.mall.common.utils.PageUtils;
import com.crr.mall.common.utils.Query;

import com.crr.mall.coupon.dao.SkuFullReductionDao;
import com.crr.mall.coupon.entity.SkuFullReductionEntity;
import com.crr.mall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    MemberPriceDao memberPriceDao;

    @Autowired
    SkuLadderDao skuLadderDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public int saveSkuReduction(SkuRelationTO skuRelationTO) {
        //保存满多少钱减多少
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        skuFullReductionEntity.setSkuId(skuRelationTO.getSkuId());
        skuFullReductionEntity.setFullPrice(skuRelationTO.getFullPrice());
        skuFullReductionEntity.setReducePrice(skuRelationTO.getReducePrice());
        if (null != skuRelationTO.getFullPrice() && skuRelationTO.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
            int i = baseMapper.saveSkuFull(skuFullReductionEntity);
            Assert.fail(i >= 1, "保存sku优惠信息失败111");
        }
        //保存会员信息
        if (null != skuRelationTO.getMemberPrice() && skuRelationTO.getMemberPrice().size() > 0) {
            int i1 = memberPriceDao.saveMemberPrice(skuRelationTO.getSkuId(), skuRelationTO.getMemberPrice());
            Assert.fail(i1 >= 1, "保存sku会员优惠信息失败22");
        }
        //满多少件减多少
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuRelationTO.getSkuId());
        skuLadderEntity.setFullCount(skuRelationTO.getFullCount());
        //        skuLadderEntity.setPrice();
        skuLadderEntity.setAddOther(skuRelationTO.getPriceStatus());
        skuLadderEntity.setDiscount(skuRelationTO.getDiscount());
        if (null != skuRelationTO.getFullCount() && skuRelationTO.getFullCount() > 0) {
            int i1 = skuLadderDao.saveLadder(skuLadderEntity);
            Assert.fail(i1 >= 1, "保存sku会员优惠信息失败333");
        }

        return 1;
    }

}